{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "db0855d0",
   "metadata": {},
   "source": [
    "# Vectara Vector Store\n",
    "In this notebook we are going to show how to use [Vectara](https://vectara.com) with LlamaIndex.\n",
    "Vectara is the first example of a \"Managed\" Index, a new type of index in Llama-index which is managed via an API."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c2d1c538",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-08T13:06:14.461233Z",
     "start_time": "2023-06-08T13:06:14.451565Z"
    }
   },
   "outputs": [],
   "source": [
    "from llama_index import SimpleDirectoryReader\n",
    "from llama_index.indices import VectaraIndex\n",
    "\n",
    "import textwrap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7010b1d-d1bb-4f08-9309-a328bb4ea396",
   "metadata": {},
   "source": [
    "### Loading documents\n",
    "Load the documents stored in the `paul_graham_essay` using the SimpleDirectoryReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c154dd4b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-08T13:06:19.009847Z",
     "start_time": "2023-06-08T13:06:19.001593Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Document ID: e0482e16-d16b-4040-8b2e-926265565b4f\n"
     ]
    }
   ],
   "source": [
    "documents = SimpleDirectoryReader(\"../data/paul_graham\").load_data()\n",
    "print(\"Document ID:\", documents[0].doc_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0232fd1",
   "metadata": {},
   "source": [
    "### Add the content of the documents into a pre-created Vectara corpus\n",
    "Here we assume an empty corpus is created and the details are available as environment variables:\n",
    "* VECTARA_CORPUS_ID\n",
    "* VECTARA_CUSTOMER_ID\n",
    "* VECTARA_API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8dfdb0a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = VectaraIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ee4473a-094f-4d0a-a825-e1213db07240",
   "metadata": {},
   "source": [
    "### Query the Vectara Index\n",
    "We can now ask questions using the VectaraIndex retriever."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0a2bcc07",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-08T13:06:29.486725Z",
     "start_time": "2023-06-08T13:06:27.565039Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[NodeWithScore(node=TextNode(id_='3a734d36-d034-46f5-8d5b-fb484fcb5cb0', embedding=None,\n",
      "metadata={'lang': 'eng', 'offset': '1166', 'len': '26'}, excluded_embed_metadata_keys=[],\n",
      "excluded_llm_metadata_keys=[], relationships={},\n",
      "hash='7227faf8ddeeb374c812b56d58fe89659f7f3e84b4ee11e88435fc69be819e0b', text=\"You had to type\n",
      "programs on punch cards, then stack them in the card reader and press a button to load the program\n",
      "into memory and run it. The result would ordinarily be to print something on the spectacularly loud\n",
      "printer. I was puzzled by the 1401. I couldn't figure out what to do with it. And in retrospect\n",
      "there's not much I could have done with it.\", start_char_idx=None, end_char_idx=None,\n",
      "text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}',\n",
      "metadata_seperator='\\n'), score=0.5630376),\n",
      "NodeWithScore(node=TextNode(id_='d3933207-63d0-4c2b-97b1-08317b83bc18', embedding=None,\n",
      "metadata={'lang': 'eng', 'offset': '1166', 'len': '26'}, excluded_embed_metadata_keys=[],\n",
      "excluded_llm_metadata_keys=[], relationships={},\n",
      "hash='7227faf8ddeeb374c812b56d58fe89659f7f3e84b4ee11e88435fc69be819e0b', text=\"You had to type\n",
      "programs on punch cards, then stack them in the card reader and press a button to load the program\n",
      "into memory and run it. The result would ordinarily be to print something on the spectacularly loud\n",
      "printer. I was puzzled by the 1401. I couldn't figure out what to do with it. And in retrospect\n",
      "there's not much I could have done with it.\", start_char_idx=None, end_char_idx=None,\n",
      "text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}',\n",
      "metadata_seperator='\\n'), score=0.5630376)]\n"
     ]
    }
   ],
   "source": [
    "query_engine = index.as_query_engine(similarity_top_k=10)\n",
    "response = query_engine.retrieve(\"What is the 1401?\")\n",
    "print(textwrap.fill(str(response[:2]), 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5dafac23",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = query_engine.query(\"What can the 1401 do?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8cf55bf7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-08T13:06:32.686121Z",
     "start_time": "2023-06-08T13:06:32.680098Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The 1401 can load programs into memory and run them. The result of running a program on the 1401\n",
      "would ordinarily be to print something on a loud printer.\n"
     ]
    }
   ],
   "source": [
    "print(textwrap.fill(str(response), 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "68cbd239-880e-41a3-98d8-dbb3fab55431",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-08T13:06:41.307336Z",
     "start_time": "2023-06-08T13:06:34.185641Z"
    }
   },
   "outputs": [],
   "source": [
    "response = query_engine.query(\"What did the author do growing up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fdf5287f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-08T13:06:44.174719Z",
     "start_time": "2023-06-08T13:06:44.163087Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The author worked on writing and programming outside of school. Specifically, they wrote short\n",
      "stories and engaged in programming activities.\n"
     ]
    }
   ],
   "source": [
    "print(textwrap.fill(str(response), 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3961b391",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
